function content0LoadFunction() {
	// Get all products to display
	x_getProducts(returnGetProducts);
	
	// Get files for PDF file drop-down menus
	x_getProductFiles(returnGetProductFiles);	
}	

function returnGetProducts(string) {	
	$('currentProducts').innerHTML = string;
}

function returnGetProductFiles(string) {
	var optionsArray = string.split('|');	
	var newOptionsArray = new Array();
	
	for(a=0;a<=((optionsArray.length)-1);a++) {
		newOptionsArray[a] = [optionsArray[a]];
	}	
	
	Ext.namespace('Ext.fileData');
	Ext.fileData.Files = newOptionsArray;
}

function clearForm() {
	$('pCat').options[0].selected = true;
	$('pTitleBox').value = '';	
	tinyMCE.getInstanceById('pShortDesc').getBody().innerHTML = '';	
	tinyMCE.getInstanceById('pContent').getBody().innerHTML = '';
	
	$('newPDFTitle').value = 'Title...';
	$('newPDFFile').value = '';
	$('attachedPDFContainer').innerHTML = '';
}

function addProduct() {
	// Turn off add button
	$('buttonNewProduct').style.display = 'none';
	
	// Remove orphaned product files
	removeOrphans();
	
	// Set formAction to 'add'
	$('formAction').value = 'add';
	
	// Clear form
	clearForm();
	
	// Populate PDF files combo-box
	var fileStore = new Ext.data.SimpleStore({
        fields: ['file'],
        data : Ext.fileData.Files
    });
    var combo = new Ext.form.ComboBox({
        store:         fileStore,
        displayField:  'file',
        typeAhead:     true,
        mode:          'local',
        triggerAction: 'all',
        emptyText:     'Select a file...',
        selectOnFocus: true,
        resizable:     true,
        shadow:        false,
        grow:          true,
        growMax:       300,
        growMin:       300,
        listWidth:     300,
        handleHeight:  12,
        name:          'newPDFFile'
    });
        	
	// Display add form
	new Effect.SlideDown('formNewProduct', {direction: 'top', duration: .3});
	
	// Set file combo box
	combo.applyTo('newPDFFile');
}

function cancelProduct() {
	// Display add form
	new Effect.SlideUp('formNewProduct', {direction: 'top', duration: .2});
	
	// Clear all form elements
	clearForm();
	
	// Turn on add button
	$('buttonNewProduct').style.display = 'block';	
}

function addFile() {
	if ($('formAction').value == 'edit') {
		var pID = $('pID').value;
	} else {
		var pID = 0;
	}
	var fileTitle = $('newPDFTitle').value;
	var fileName  = $('newPDFFile').value;
	if ( (fileTitle == 'Title...') || (fileTitle == '') ) {
		// TODO: Show error...we need a title
	} else {
		x_addFile(fileTitle,fileName,0,pID,returnAddFile);
	}	
}

function returnAddFile(string) {
	stringArray = string.split("|");
	var fileID    = stringArray[0];
	var fileTitle = stringArray[1];
	var fileName  = stringArray[2];
	var fileSize  = stringArray[3];
	
	var string = '<a href="#" onClick="removeFile('+fileID+',\'attachedPDF\'); return false;" title="Remove file">';
	string    += '<img src="images/iconError.gif" style="border: none;" alt="Remove file" /></a>&nbsp;';
	string    += '<a href="'+fileName+'" target="_blank">'+fileTitle+'</a> '+fileSize;
	
	var attachedPDFContainer = $('attachedPDFContainer');
	var newPDFDiv = document.createElement('div');
	
	newPDFDiv.id = 'attachedPDF'+fileID;
	newPDFDiv.style.height = '25px';
	
	newPDFDiv.innerHTML = string;
			
	attachedPDFContainer.appendChild(newPDFDiv);
}

function removeFile(fileID,elementName) {
	var PDFDivID = elementName+fileID;
	$(PDFDivID).style.display = 'none';
	x_removeFile(fileID,returnRemoveFile);
}

function returnRemoveFile(fileID) {
	// Do nothing here
}

function saveProduct() {
	var pID = $('pID').value;
	var formAction = $('formAction').value;
	var pCat   = $('pCat').options;
	var pCat   = pCat[pCat.selectedIndex].value;
	var pTitle     = $('pTitleBox').value;
	var pShortDesc = tinyMCE.getContent('pShortDesc');
	var pContent   = tinyMCE.getContent('pContent');	
	
	var phpString  = pCat+'~~|~~'+pTitle+'~~|~~'+pShortDesc+'~~|~~'+pContent;
	if (pTitle == '') {
		// Error
		var message = 'You must enter a product title.';
		updateStatus(message,'error','statusDiv');
	} else {
		x_saveProduct(phpString,formAction,pID,returnSaveProduct);	
	}	
}

function returnSaveProduct() {
	// Close add product form
	cancelProduct();
	var message = 'New product has been successfully added.';
	updateStatus(message,'success','statusDiv');	
	x_getProducts(returnGetProducts);
}

function returnDeleteProduct() {
	var message = 'Product has been deleted.';
	updateStatus(message,'success','statusDiv');
	x_getProducts(returnGetProducts);
}

function editProduct(pID) {
	x_getProductForEdit(pID,returnGetProductForEdit);
}

function returnGetProductForEdit(string) {
	// Parse string
	stringArray = string.split("|");
	var pID        = stringArray[0];
	var pCat       = stringArray[1];
	var pTitle     = stringArray[2];
	var pShortDesc = stringArray[3];
	var pContent   = stringArray[4];
	$('attachedPDFContainer').innerHTML = stringArray[5]; // <-- File list
	
	// Populate add/edit form	
	var pCatOptions = $('pCat').options;
	for(a=0;a<pCatOptions.length;a++) {
		if ($('pCat').options[a].value == pCat) {
			$('pCat').options[a].selected = true;
		}
	}
	
	$('pTitleBox').value = pTitle;
	
	tinyMCE.getInstanceById('pShortDesc').getBody().innerHTML = pShortDesc;	
	tinyMCE.getInstanceById('pContent').getBody().innerHTML = pContent;
	
	// Set formAction to 'edit'
	$('formAction').value = 'edit';
	
	// Set pID
	$('pID').value = pID;
		
	// Turn off add button
	$('buttonNewProduct').style.display = 'none';
	
	// Populate PDF files combo-box
	var fileStore = new Ext.data.SimpleStore({
        fields: ['file'],
        data : Ext.fileData.Files
    });
    var combo = new Ext.form.ComboBox({
        store:         fileStore,
        displayField:  'file',
        typeAhead:     true,
        mode:          'local',
        triggerAction: 'all',
        emptyText:     'Select a file...',
        selectOnFocus: true,
        resizable:     true,
        shadow:        false,
        grow:          true,
        growMax:       300,
        growMin:       300,
        listWidth:     300,
        handleHeight:  12,
        name:          'newPDFFile'
    });
        	
	// Display add form
	new Effect.SlideDown('formNewProduct', {direction: 'top', duration: .3});
	
	// Set file combo box
	combo.applyTo('newPDFFile');	
	$('content0').scrollTop = 0;	
}

function removeOrphans() {
	x_removeOrphans(returnRemoveOrphans);
}

function returnRemoveOrphans() {
	// Do nothing here
}